package handler

import (
	"cargo-hub/cache"
	"cargo-hub/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func RepoAdd(c *gin.Context) {
	repo_id := c.PostForm("repo_id")
	repo_code := c.PostForm("repo_code")
	repo_name := c.PostForm("repo_name")
	if repo_id == "" || repo_code == "" || repo_name == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Repo{
		RepoId:   repo_id,
		RepoCode: repo_code,
		RepoName: repo_name,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": data,
	})
	return

}
func OrderRepoAdd(c *gin.Context) {
	order_id := c.PostForm("order_id")
	order_code := c.PostForm("order_code")
	order_name := c.PostForm("order_name")
	shop := c.PostForm("shop")
	rid := c.PostForm("rid")
	if order_id == "" || order_code == "" || order_name == "" || shop == "" || rid == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.OrderRepo{
		OrderId:   order_id,
		OrderCode: order_code,
		OrderName: order_name,
		Shop:      shop,
		Rid:       rid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func OrderRepoList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "2"))
	offset := (page - 1) * size
	var order []models.OrderRepoList
	redis_key := "order_repo_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("order_repos").
			Select(
				"order_repos.order_id AS OrderId",
				"order_repos.order_code AS OrderCode",
				"order_repos.order_name AS OrderName",
				"order_repos.shop AS Shop",
				"order_repos.rid AS Rid",
				"repos.repo_id AS RepoId",
				"repos.repo_code AS RepoCode",
				"repos.repo_name AS RepoName",
			).Joins("LEFT JOIN repos ON repos.id = order_repos.rid").
			Offset(offset).Limit(size).Find(&order)
		jsonData, _ := json.Marshal(&order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	var total int64
	models.DB.Table("order_repos").Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  order,
		"total": total,
	})
	return
}
func OrderRepoDetail(c *gin.Context) {
	id := c.Query("id")
	var order models.OrderRepoList
	redis_key := "order_repo_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("order_repos").
			Select(
				"order_repos.order_id AS OrderId",
				"order_repos.order_code AS OrderCode",
				"order_repos.order_name AS OrderName",
				"order_repos.shop AS Shop",
				"order_repos.rid AS Rid",
				"repos.repo_id AS RepoId",
				"repos.repo_code AS RepoCode",
				"repos.repo_name AS RepoName",
			).Joins("LEFT JOIN repos ON repos.id = order_repos.rid").
			Where(" order_repos.id =?", id).Find(&order)
		jsonData, _ := json.Marshal(&order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &order)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	return
}
